package service.impl;

import dao.CaseDao;
import dao.MakeDao;
import dao.PrescriptionDao;
import dao.ScheduleDao;
import entity.Case;
import entity.Make;
import entity.Patient;
import entity.Prescription;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.CaseService;
import util.Random;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service("caseService")
public class CaseServiceImpl implements CaseService {

    @Autowired
    private CaseDao caseDao;

    @Autowired
    private MakeDao makeDao;

    @Autowired
    private ScheduleDao scheduleDao;

    @Autowired
    private PrescriptionDao prescriptionDao;

    @Override
    public List<Case> query(Case c,int pageSize, int pageNumber) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("nName",c.getnName());
        map.put("cVisitTime",c.getcVisitTime());
        map.put("offset",(pageNumber-1)*pageSize);
        map.put("limit",pageSize);
        return caseDao.query(map);
    }

    @Override
    public boolean AddCase(Case c,String sid,String prescription,String mid,String money) {
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
       String date= format.format(new Date());
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("endDate",date);
        map.put("mid",mid);
        Prescription p=new Prescription();
         if(caseDao.add(c)>0){
             p.setpId(c.getpId());
             p.setcId(caseDao.GetIdByCase());
             p.setPrescription(prescription);
             String[] preStr=prescription.split(":");
             for(int i=0;i<preStr.length;i++){
                 System.out.println(preStr[i]);
             }
             p.setnId(c.getnId());
             p.setMoney(Double.parseDouble(money));
             if(scheduleDao.UpDateByStat(sid,"3")>0 && makeDao.UpDateByStat(map)>0 && prescriptionDao.Add(p)>0 ){
                    return true;
             }
         }
        return false;
    }
}
